<?php
require_once('inc/init.php');
require_once('inc/pager.php');
require_once getCacheFilePath('forums.php');
require_once getCacheFilePath('announces.php');
require_once getCacheFilePath('ads.php');
$fid=numFilter($_GET['fid']);
$_SYS['forumurl']=genUrl("forum.php?fid={$fid}");
//var_dump($_SERVER);
$forumpass=strFilter($_POST['forumpass']);
if(!empty($forumpass)){
	setCookies("forumpass_{$fid}", $forumpass);
	_header_("location:forum.php?fid={$fid}");
}

$orderby=strFilter($_GET['orderby']);
$d=numFilter($_GET['d']);
$condition="fid={$fid} and isrecycle=0 and verifystate=0 and isalltop=0";


switch($orderby){
	case "replynum":
		$orderstr="replynum desc";
	break;
	case "hits":
		$orderstr="hits desc";
	break;
	case "author":
		$orderstr="userid";
	break;
	case "posttime":
		$orderstr="posttime desc";
	break;
	case "lastreply":
		$d=empty($d)?604800:$d;
		$timed = time()-$d;
		$condition.=" and ordertime>{$timed}";
		$orderstr="ordertime desc";
	break;
	case "isgood":
		$orderstr="isgood desc";
	break;
	default:
		$orderstr="istop desc, ordertime desc";
	break;
}


$curPage = numFilter($_GET["page"]);
$forum=$db->row_select_one("forums","id={$fid}");
if(empty($forum)){
	printMsg('system_noforum');
}
$cache_forum = $cache_forums[$fid];
if(intval($cache_forum['pid'])==0){
	_header_("location:index.php?fid={$cache_forum['id']}");
}
$headtitle=strip_tags($cache_forum['forumname']);
$headkeywords=$headtitle.",".(empty($cache_forum['metakeywords'])?"":$cache_forum['metakeywords'].",");
$headdesc=htmlFilter(str_replace(array("\r", "\n"), array('', ''), $forum['forumcontent']));

if(!empty($forum['forumusers']) && !in_array($lg['username'],explode("\n",$forum['forumusers'])) && !isForumAdmin($fid)){
	printMsg('forum_forumusers');
}

if(!empty($forum['forumgroups']) && !in_array($lg['groupid'],explode(",",$forum['forumgroups'])) && !isForumAdmin($fid)){
	printMsg('forum_forumgroups');
}

if(!empty($forum['forumpass']) && getCookies("forumpass_{$fid}")!=$forum['forumpass'] && !isForumAdmin($fid)){
	printMsg('forum_forumpass');
}

$pagerlink="forum.php?fid={$fid}";
$pagerlink.=empty($orderby)?"":"&orderby={$orderby}";
$pagerlink.=empty($d)?"":"&d={$d}";
$pagerlink.="&page={page}";
if(empty($ordery) && empty($d)){$pagerlink=genUrl($pagerlink);}
$pager = new Pager();
$pager->init($cache_settings['topicspernum'],$curPage,$pagerlink);
$topics = $pager->queryRows($db,"topics",$condition, "*",$orderstr,$forum['topicsnum']);

if(count($cache_forum['childfid'])>0){
	$forums_rows=$db->row_select("forums","pid={$fid}",0,"id,todaynum,topicsnum,postsnum,lastuser,lastpost,lastposttime,lasttid,admins,picurl,forumpass,forumusers,forumgroups");
	foreach($forums_rows as $forum_row){
		$cache_forums[$forum_row['id']]['todaynum']=$forum_row['todaynum'];
		$cache_forums[$forum_row['id']]['topicsnum']=$forum_row['topicsnum'];
		$cache_forums[$forum_row['id']]['postsnum']=$forum_row['postsnum'];
		$cache_forums[$forum_row['id']]['lastuser']=$forum_row['lastuser'];
		$cache_forums[$forum_row['id']]['lastpost']=htmlFilter(cutStr($forum_row['lastpost'],30));
		$cache_forums[$forum_row['id']]['lastposttime']=empty($forum_row['lastposttime'])?"":getDateStr($forum_row['lastposttime']);
		$cache_forums[$forum_row['id']]['lasttid']=$forum_row['lasttid'];
		$cache_forums[$forum_row['id']]['adminsstr']=getAdminStr($forum_row['admins']);
		$cache_forums[$forum_row['id']]['forumicon']=empty($forum_row['picurl'])?($_SYS['TP'].( (!empty($forum_row['forumpass']) || !empty($forum_row['forumusers']) || !empty($forum_row['forumgroups']) )? "/images/ico_haslock.png"  : ($forum_row['todaynum']>0?"/images/ico_hasnew.png":"/images/ico_hasnonew.png"))):$forum_row['picurl'];
	}
}

$forum_ancarr=array();
$forum_alltop=array();
$annouceNum=0;
if($curPage<=1){
	$announces_arr = array();
	if(is_array($cache_announces_tree['all'])){
		$announces_arr=array_merge($announces_arr, $cache_announces_tree['all']);
	}
	if(is_array($cache_announces_tree[$fid])){
		$announces_arr=array_merge($announces_arr, $cache_announces_tree[$fid]);
	}
	
	foreach($announces_arr as $ancid){
		$announce=$cache_announces[$ancid];
		if($announce['starttime']>$_SYS['time'] || ($announce['stoptime']<$_SYS['time'] && $announce['stoptime']>0)){
			continue;
		}
		$annouceNum++;
		$announce['posttime']=getDateStr($announce['posttime']);
		$announce['title']=htmlFilter($announce['title']);
		$announce['anclink'] = numFilter($announce['type'])==1? $announce['url']:"announce.php?id=$announce[id]";
		array_push($forum_ancarr, $announce);
	}

	$forum_alltop=$db->row_select("topics","isalltop=1",0,"*","ordertime desc");
	$topics=array_merge($forum_alltop, $topics);
}	

foreach($topics as $key=>$topic){
	$topic['posttime']=getDateStr($topic['posttime']);
	$topic['title']=htmlFilter($topic['title']);
	$highlight=$topic['highlight'];
	if(!empty($highlight)){
		$topic['title']=genHighLighStr($highlight,$topic['title']);
	}
	if($topic['isresolved']==1){
		$topic['title'].=" <span class='isresolved'>[已解决]</span>";
	}

	$lastre=explode("\t",$topic['lastreply']);
	$topic['topicuser']=htmlFilter($lastre[1]);
	$topic['topiclasttime'] = getDateStr($lastre[0]);
	$topic['icon']=getTopicIcon($topic);
	$topic['addstr'].=(empty($topic['attachicon'])?"":"<span class=\"ext_small ext_small_{$topic['attachicon']}\"><i>&nbsp;</i></span>");
	$topic['viewurl'] = genUrl("view.php?tid={$topic['id']}");
	$topic['userurl'] = genUrl("user.php?uid={$topic['userid']}");
	
	if( $topic['replynum']/$cache_settings['postspernum']>1){
		$topic['addstr'].="<span class=\"forum_smallpager\">".$pager->getSmallPager($cache_settings['postspernum'], $topic['replynum']+1, 5, genUrl("view.php?tid={$topic['id']}&page={page}"))."</span>";
	}
	$topics[$key]=$topic;
}

$adcode=getAd($fid);
$adminsstr=getAdminStr($forum['admins']);

require_once('header.php');
require_once getTemplatePath('forum.htm');
footer();
?>